package com.pan.alg.niuke.link;

import java.util.ArrayList;
import java.util.List;

public class ListNodeUtil {

    public static ListNode transArr2List(int[] arr){
        ListNode dumy = new ListNode(0);
        ListNode current = dumy;
        for (int i : arr) {
            ListNode listNode = new ListNode(i);
            current.next=listNode;
            current=listNode;
        }
        return dumy.next;
    }

    public static void printListNode(ListNode listNode){
        int index=0;
        while(listNode!=null){
            System.out.println("当前坐标:"+(++index)+"-->"+listNode.val);
            listNode=listNode.next;
        }
    }

    public static ListNode reverseLinkNode(ListNode pHead) {
        if(null==pHead){
            return pHead;
        }
        if(null==pHead.next){
            return pHead;
        }

        ListNode backHead = pHead;
        ListNode currentNode = pHead;
        ListNode nextNode = currentNode.next;
        pHead.next=null;
        while(nextNode!=null){
            currentNode = nextNode;
            nextNode = currentNode.next;
            currentNode.next=backHead;
            backHead=currentNode;
        }
        return backHead;
    }

    public static Integer getListSize(ListNode pHead) {
        int index=0;
        while(pHead!=null){
            index++;
            pHead=pHead.next;
        }
        return index;
    }

    public static ListNode getNthNode(ListNode pHead,Integer k){
        int index=0;
        ListNode returnNode = null;
        while(pHead!=null){
            index++;
            if(index==k){
                returnNode=pHead;
                break;
            }
            pHead=pHead.next;
        }

        return returnNode;
    }

    public static List<ListNode> transListNode2List(ListNode head1) {
        List<ListNode> listNodes = new ArrayList<>();
        while(head1!=null){
            listNodes.add(head1);
            head1=head1.next;
        }
        return listNodes;
    }

    public static List<ListNode> sortListNode(List<ListNode> listNodes) {


        return listNodes;
    }

    public static void main(String[] args) {
        int[] arr ={1,2,3,4,5};
        ListNode arr2List = transArr2List(arr);
        printListNode(arr2List);
        ListNode reverse = reverseLinkNode(arr2List);
        printListNode(reverse);
    }

    public static ListNode transListNode2LinkNode(List<ListNode> sortedListNode) {
        if(null==sortedListNode||sortedListNode.isEmpty()){
            return null;
        }
        ListNode head = sortedListNode.get(0);
        sortedListNode.get(sortedListNode.size()-1).next=null;
        for (int i = 1; i < sortedListNode.size(); i++) {
            head.next=sortedListNode.get(i);
            head=head.next;
        }
        return sortedListNode.get(0);
    }
}
